package cn.zhpdmmz.service.impl;

import cn.zhpdmmz.entity.CheckInformation;
import cn.zhpdmmz.entity.CheckInformationDetails;
import cn.zhpdmmz.entity.Customer;
import cn.zhpdmmz.mapper.*;
import cn.zhpdmmz.service.ICheckInformationDetailsService;
import cn.zhpdmmz.utils.CreatCheckInNo;
import cn.zhpdmmz.utils.DateUtils;
import cn.zhpdmmz.vo.CheckInVo;
import org.springframework.stereotype.Service;
import cn.zhpdmmz.utils.WeekendUtils;
import javax.annotation.Resource;
import java.util.*;

/**
 * (CheckInformationDetails)表服务实现类
 *
 * @author CJW
 * @since 2022-03-26 17:22:05
 */
@Service("checkInformationDetailsService")
public class CheckInformationDetailsServiceImpl implements ICheckInformationDetailsService {
    @Resource
    private CheckInformationDetailsMapper checkInformationDetailsMapper;
    @Resource
    private CheckInformationMapper checkInformationMapper;
    @Resource
    private HotelRoomMapper hotelRoomMapper;
    @Resource
    private  HotelRoomTypeMapper hotelRoomTypeMapper;

    private CheckInformation checkInformation;
    @Resource
    private RoomStatusMapper roomStatusMapper;
    private String checkInNo,roomNo,breakFast,customerType;
    private int days;
    Date startTime;
    Map<String,List<Customer>> mapCache = new HashMap<>(); //缓存数据

    /**
     * 通过ID查询单条数据
     *
     * @param checkInformationDetailsId 主键
     * @return 实例对象
     */
    @Override
    public CheckInformationDetails queryById(Integer checkInformationDetailsId) {
        return this.checkInformationDetailsMapper.queryById(checkInformationDetailsId);
    }

    /**
     * 查询指定行
     *
     * @param checkInformationDetails 筛选条件
     * @return 查询结果
     */
    @Override
    public List<CheckInformationDetails> queryAll(CheckInformationDetails checkInformationDetails) {
        return this.checkInformationDetailsMapper.queryAll(checkInformationDetails);
    }

    /**
     * 新增数据
     *
     * @param checkInformationDetails 实例对象
     * @return 实例对象
     */
    @Override
    public CheckInformationDetails insert(CheckInformationDetails checkInformationDetails) {
        this.checkInformationDetailsMapper.insert(checkInformationDetails);
        return checkInformationDetails;
    }

    /**
     * 修改数据
     *
     * @param checkInformationDetails 实例对象
     * @return 实例对象
     */
    @Override
    public CheckInformationDetails update(CheckInformationDetails checkInformationDetails) {
        this.checkInformationDetailsMapper.update(checkInformationDetails);
        return this.queryById(checkInformationDetails.getCheckInformationDetailsId());
    }

    /**
     * 通过主键删除数据
     *
     * @param checkInformationDetailsId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer checkInformationDetailsId) {
        return this.checkInformationDetailsMapper.deleteById(checkInformationDetailsId) > 0;
    }
    /**
     * 生成缓存数据_内存存储_HashMap
     * 步骤1_依赖方法createMapCache
     * @param checkInNo
     * @return
     */
    private Map<String, List<Customer>> createMapCache(String checkInNo){

        List<Customer> customerList = new ArrayList<>();
        mapCache.put(checkInNo,customerList);
        return mapCache;
    }

    /**
     * 步骤一
     * 添加客户
     *
     * @param checkInVo 主键
     * @return 是否成功
     */
    @Override
    public Map<String, Object> addCustomer(CheckInVo checkInVo) {
        if(checkInNo == null){
            CreatCheckInNo creatCheckInNo = new CreatCheckInNo();
            checkInNo = creatCheckInNo.newCheckInNo();
            checkInformation = new CheckInformation();
            checkInformation.setCheckInformationNo(checkInNo);
            startTime = new Date();
            checkInformation.setStartTime(startTime);
            this.checkInformationMapper.insert(checkInformation);
            this.createMapCache(checkInNo);
        }

        //待入住顾客列表
        List<Customer> customerList = mapCache.get(checkInNo);
        //新的顾客
        Customer customer = new Customer();
        //根据最后一位数判断男女
        String identifyCard;
        identifyCard = checkInVo.getIdentifyCard();
        if(Integer.parseInt(identifyCard) % 2 == 0){
            customer.setCustomerSex("女");
        }else
            customer.setCustomerSex("男");

        customer.setCustomerName(checkInVo.getCustomerName());
        customer.setIdentifyCard(checkInVo.getIdentifyCard());

        //将新顾客添加到列表里
        customerList.add(customer);
        mapCache.put(checkInNo,customerList);

        //显示缓存信息
        Map<String, Object> info = new HashMap<String,Object>();
        info.put("customerList",customerList);
        return info;
    }

    /**
     * 步骤1_1：取消
     *
     * @return 实例对象
     */
    @Override
    public boolean cancelAddCustomer(){
        this.checkInformationMapper.deleteByCheckInNo(checkInNo);
        this.removeMapCache(checkInNo);
        checkInNo = null;
        return true;
    }
    /**
     * 删除缓存数据_内存存储_HashMap
     * 步骤2_依赖方法1
     * @param checkInNo
     */
    private void removeMapCache(String checkInNo) {
        removeMap(checkInNo, mapCache);
    }

    static void removeMap(String checkInNo, Map<String, List<Customer>> mapCache) {
        Iterator<Map.Entry<String,List<Customer>>> iterator = mapCache.entrySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next().getKey();
            if (checkInNo.equals(key)) {
                iterator.remove();
            }
        }
    }

    /**
     * 步骤二：选择房间和入住天数
     * 办理入住
     *
     *
     * @return 实例对象
     */
    @Override
    public boolean checkIn(CheckInVo checkInVo) {
        boolean flag, tag;
        roomNo = checkInVo.getRoomNo();
        breakFast = checkInVo.getBreakfast();
        days = checkInVo.getDays();
        customerType = checkInVo.getCustomerType();
        List<CheckInformationDetails> checkInformationDetailsList = new ArrayList<>();
        List<Customer> customerList = mapCache.get(checkInNo);
        System.out.println(customerList.size());

        int statusId = this.hotelRoomMapper.queryTypeByRoom(roomNo).getStatusId();
        if (!this.roomStatusMapper.queryById(statusId).getStatusName().equals("空房")) {
            this.checkInformationMapper.deleteByCheckInNo(checkInNo);
            this.removeMapCache(checkInNo);
            checkInNo = null;
            return false;
        }else {
            Date endTime;
            endTime = DateUtils.rollDay(startTime, days);
            for (int i = 0; i < customerList.size(); i++) {
                //插入订单明细表
                CheckInformationDetails checkInformationDetails = new CheckInformationDetails();
                checkInformationDetails.setRoomNo(roomNo);
                checkInformationDetails.setCustomerName(customerList.get(i).getCustomerName());
                checkInformationDetails.setBreakfast(breakFast);
                checkInformationDetails.setStartTime(startTime);
                checkInformationDetails.setEndTime(endTime);
                checkInformationDetails.setCheckInformationId(this.checkInformationMapper.queryIdByCheckInNo(checkInNo).getCheckInformationId());
                checkInformationDetailsList.add(checkInformationDetails);
            }
            flag = this.checkInformationDetailsMapper.insertBatch(checkInformationDetailsList) > 0;

            //插入订单表
            checkInformation.setPayStatus("未支付");
            checkInformation.setRoomNo(roomNo);
            checkInformation.setCustomerType(customerType);
            checkInformation.setPeopleNumber(customerList.size());
            double amount = 0;
            if(breakFast.equals("否")) {
                for(int i =0; i < days; i++){
                    if(WeekendUtils.weekendPrice(DateUtils.rollDay(startTime,i),roomNo)){
                        amount += (this.hotelRoomTypeMapper.queryPriceById(this.hotelRoomMapper.queryTypeByRoom(roomNo).getRoomTypeId()).getPrice() + 60);
                    }
                    else{
                        amount += this.hotelRoomTypeMapper.queryPriceById(this.hotelRoomMapper.queryTypeByRoom(roomNo).getRoomTypeId()).getPrice();
                    }
                }

            }
            else{
                for(int i =0; i < days; i++){
                    if(WeekendUtils.weekendPrice(DateUtils.rollDay(startTime,i),roomNo)){
                        amount += (this.hotelRoomTypeMapper.queryPriceById(this.hotelRoomMapper.queryTypeByRoom(roomNo).getRoomTypeId()).getPrice() + 28*customerList.size() + 60);
                    }
                    else{
                        amount += (this.hotelRoomTypeMapper.queryPriceById(this.hotelRoomMapper.queryTypeByRoom(roomNo).getRoomTypeId()).getPrice() + 28*customerList.size());
                    }
                }
            }

            checkInformation.setTotalAmount(amount);

            tag = this.checkInformationMapper.update(checkInformation) > 0;
        }
            //更改房间状态
            if (flag && tag) {
                this.removeMapCache(checkInNo);
                checkInNo = null;
                return true;
            }
            return false;
        }
}
